<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-12-30 16:11:12
 * @Last Modified time: 2023-02-23 10:31:27
 */
namespace app\sys\controller\store;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\store\DiyPageService;
use app\common\utils\store\DiyPageUtils;
use app\common\services\store\GoodsCateService;

/**
 * 商城-自定义页面
 * Class DiyPage 
 * @package app\sys\controller\store
 */
class DiyPage extends Base
{
    /**
     * DiyPage constructor.
     * @param App $app
     */
    public function __construct(App $app,DiyPageService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 自定义页面列表
     * @method(GET)
     */
    public function list()
    {
        $param = $this->request->getMore([]);
        $this->success($this->service->getDiyPageList($param));
    }

    /**
     * 自定义页面详细 
     * @noAuth(true)
     * @method(GET)
     */
    public function detail(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $this->success($this->service->getDiyPageDetail($id));
    }

    /**
     * 添加自定义页面
     * @method(POST)
     */
    public function create(){
        $pageData = $this->request->param('page_data');
        if (empty($pageData['page']['title']['text'])) $this->error('页面标题不能为空');

        if($this->service->save([
            'page_name'=>$pageData['page']['title']['text'],
            'page_data'=>$pageData
        ])){
            $this->success('添加成功!');
        }
        $this->error('添加失败!');
    }


    /**
     * 编辑自定义页面
     * @method(PUT)
     */
    public function update()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $pageData = $this->request->param('page_data');
        if (empty($pageData['page']['title']['text'])) $this->error('页面标题不能为空');

        if($this->service->update($id, [
            'page_name'=>$pageData['page']['title']['text'],
            'page_data'=>$pageData
        ])){
            $this->service->deleteCache();
            $this->success('修改成功!');
        }
        $this->error('修改失败!');
    }

    /**
     * 删除自定义页面
     * @method(DELETE)
     */
    public function delete()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }

        if ($this->service->delete((int)$id)) {
            $this->service->deleteCache();
            $this->success('删除成功!');
        } 
        $this->error('删除失败!');
    }

    /**
     * 设置为首页
     * @method(PUT)
     */
    public function setHome()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }

        if ($this->service->setHome((int)$id)) {
            $this->service->deleteCache();
            $this->success('设置成功!');
        } 
        $this->error('设置失败!');
    }

    /**
     * 自定义页面Home详细 
     * @noAuth(true)
     * @method(GET)
     */
    public function getHome(){
        $this->success($this->service->getHome());
    }

    /**
     * 获取diy表单默认数据 
     * @noAuth(true)
     * @method(GET)
     */
    public function defaultData(){
        $param = $this->request->getMore([
            ['tools_type','page']
        ]);
    	$this->success([
    		'items'=>DiyPageUtils::getDefaultItems(),
            'data'=>DiyPageUtils::getDefaultData(),
            'tools'=>DiyPageUtils::getToolsData($param['tools_type']),
            'opts'=>[
                'goods_cate'=>GoodsCateService::instance()->getGoodsCateList()
            ]
    	]);
    }

    /**
     * 获取diy链接数据
     * @noAuth(true)
     * @method(GET)
     */
    public function linkList(){
        $list = [];
        $type = $this->request->param('type');
        switch ($type) {
            case 'diy':
                $list = $this->diyPageListData();
                break;
            case 'default':
                $list =  DiyPageUtils::getLinkLists();
                break;
            case 'goods_cate':
                $list = $this->GoodsCateData();
                break;
            case 'goods':
                $list = $this->GoodsData();
                break;
        }
        $this->success('获取成功',$list);
    }

    /**
     * 获取diy模板数据
     * @noAuth(true)
     * @method(GET)
     */
    public function tempList(){
        $type = $this->request->param('type');
        $this->success('获取成功',DiyPageUtils::getTempLists($type));
    }

    /**
     * 获取分类风格数据
     * @method(GET)
     */
    public function getCategroy(){
        $this->success('获取成功',[
            'data'=> DiyPageUtils::getCategroy(),
            'layout'=>sysconf('store_diy_categroy')
        ]);
    }

    /**
     * 修改分类风格
     * @method(PUT)
     */
    public function updateCategroy(){
        if (!$layout = $this->request->param('layout')) {
            $this->error('参数错误!');
        }
        sysconf('store_diy_categroy',$layout);
        $this->success('修改成功',[
            'data'=> DiyPageUtils::getCategroy(),
            'layout'=>$layout
        ]);
    }

    /**
     * 获取风格主题数据
     * @method(GET)
     */
    public function getTheme(){
        $this->success('获取成功',[
            'data'=> DiyPageUtils::getThemes(),
            'theme'=>sysconf('store_diy_theme')
        ]);
    }

    /**
     * 修改风格主题
     * @method(PUT)
     */
    public function updateTheme(){
        if (!$theme = $this->request->param('theme')) {
            $this->error('参数错误!');
        }
        sysconf('store_diy_theme',$theme);
        $this->success('修改成功',[
            'data'=> DiyPageUtils::getThemes(),
            'theme'=>$theme
        ]);
    }

    /**
     * 获取个人中心数据
     * @method(GET)
     */
    public function getUser(){
        $this->success(sysconf('store_diy_user|json'));
    }

    /**
     * 修改个人中心
     * @method(PUT)
     */
    public function updateUser(){
        if (!$diyUser = $this->request->param('diy_user')) {
            $this->error('参数错误!');
        }
        sysconf('store_diy_user',json_encode($diyUser,JSON_UNESCAPED_UNICODE));
        $this->success('修改成功',$diyUser);
    }

}
